/**
 * 速判题目生成器
 */
import QuestionGeneratorBase from './question-generator-base.js';

// 预设分数池 (1/20 到 1/2)
const FRACTION_POOL = [
    {numerator: 1, denominator: 20, decimal: 0.05},
    {numerator: 1, denominator: 19, decimal: 0.0526},
    {numerator: 1, denominator: 18, decimal: 0.0556},
    {numerator: 1, denominator: 17, decimal: 0.0588},
    {numerator: 1, denominator: 16, decimal: 0.0625},
    {numerator: 1, denominator: 15, decimal: 0.0667},
    {numerator: 1, denominator: 14, decimal: 0.0714},
    {numerator: 1, denominator: 13, decimal: 0.0769},
    {numerator: 1, denominator: 12, decimal: 0.0833},
    {numerator: 1, denominator: 11, decimal: 0.0909},
    {numerator: 1, denominator: 10, decimal: 0.1},
    {numerator: 1, denominator: 9, decimal: 0.1111},
    {numerator: 1, denominator: 8, decimal: 0.125},
    {numerator: 1, denominator: 7, decimal: 0.1429},
    {numerator: 1, denominator: 6, decimal: 0.1667},
    {numerator: 1, denominator: 5, decimal: 0.2},
    {numerator: 2, denominator: 9, decimal: 0.2222},
    {numerator: 1, denominator: 4, decimal: 0.25},
    {numerator: 2, denominator: 7, decimal: 0.2857},
    {numerator: 1, denominator: 3, decimal: 0.3333},
    {numerator: 3, denominator: 8, decimal: 0.375},
    {numerator: 2, denominator: 5, decimal: 0.4},
    {numerator: 3, denominator: 7, decimal: 0.4286},
    {numerator: 1, denominator: 2, decimal: 0.5}
];

class SpeedJudgeGenerator extends QuestionGeneratorBase {
    /**
     * 生成速判题目
     * @param {number} count 题目数量，默认5题
     * @returns {Object} 题目对象
     */
    static generateQuestions(count = 5) {
        // 随机选择指定数量的分数
        const selectedFractions = this.shuffleArray([...FRACTION_POOL]).slice(0, count);

        // 创建分数和小数数组，使用预定义的精确小数值
        const fractions = selectedFractions.map(f => ({
            id: `f_${f.numerator}_${f.denominator}`,
            display: `${f.numerator}/${f.denominator}`,
            value: f.decimal, // 使用预定义的精确小数值
            numerator: f.numerator,
            denominator: f.denominator
        }));

        const decimals = selectedFractions.map(f => ({
            id: `d_${f.numerator}_${f.denominator}`,
            display: this.formatDecimal(f.decimal), // 使用格式化后的显示
            value: f.decimal // 使用相同的精确小数值
        }));

        // 打乱小数数组顺序
        const shuffledDecimals = this.shuffleArray([...decimals]);

        // 创建正确配对映射
        const correctPairs = fractions.map(fraction => ({
            fractionId: fraction.id,
            decimalId: decimals.find(d => d.value === fraction.value).id
        }));

        return {
            id: Date.now().toString(),
            fractions,
            decimals: shuffledDecimals,
            correctPairs,
            userAnswers: [],
            score: 0,
            isCompleted: false,
            createdAt: new Date().toISOString()
        };
    }
}

export default SpeedJudgeGenerator;
export { FRACTION_POOL };

